import http from '@/util/http'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isAsidesShow: true,
    indexListData: [],
    totalCount: 0,
    cityName: '北京市',
    cityData: [],
    shopCarList: {},

    danjia: 0
  },
  mutations: {
    setIndexData (state, indexData) {
      state.indexListData = [...state.indexListData, ...indexData]
    },
    showAsides (state) {
      state.isAsidesShow = true
    },
    hideAsides (state) {
      state.isAsidesShow = false
    },
    setClearData (state) {
      state.indexListData = []
    },
    setIndexPage (state, count) {
      state.totalCount = count
    },
    setCitydata (state, city) {
      state.cityData = city
    },
    setCityName (state, name) {
      state.cityName = name
    },
    setDanjia (state, price) {
      state.danjia = price
    },
    setShopCarData (state, data) {
      state.shopCarList = data
    }
  },
  actions: {
    getShopCarList (store, id) {
      return http({
        url: `/heihei/api/product/sku/sku_stock_detail?c=h5&s=20000&t=1605158824839&v=1.0&product_id=${id}&stock_type=1`
      }).then(res => {
        // console.log(res.data.data.skuMap)
        store.commit('setDanjia', res.data.data.skuLinePriceRange[0])
        store.commit('setShopCarData', res.data.data.skuMap)
      })
    },
    getIndexListData (store, id) {
      return http({
        url: `/heihei/api/product/product/product_list?c=h5&s=20000&t=1605163519340&v=1.0&category_id=${id}&sort_field=1&sort_order=2&pn=1&ps=6`
      }).then(res => {
        store.commit('setIndexData', res.data.data.pageList)
        store.commit('setIndexPage', res.data.data.totalCount)
      })
    },
    getCityData (store) {
      return fetch('../../../city.json').then(res => res.json()).then(res => {
        // console.log(res)
        store.commit('setCitydata', res.rows)
      })
    }
  },
  modules: {
  }
})
